package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.constans.RedisConstant;
import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.model.product.BaseCategory2;
import com.atguigu.gmall.model.product.BaseCategory3;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.to.BaseCategroysTo;
import com.atguigu.gmall.product.mapper.BaseCategory1Mapper;
import com.atguigu.gmall.product.mapper.BaseCategory2Mapper;
import com.atguigu.gmall.product.mapper.BaseCategory3Mapper;
import com.atguigu.gmall.serviceutil.annotation.AppCache;
import com.atguigu.gmall.serviceutil.service.CacheService;
import com.atguigu.gmall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/8/26 21:29
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;
    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;
    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;
    @Autowired
    private CacheService cacheService;
    @AppCache(cacheKey ="category:level:1")
    @Override
    public List<BaseCategory1> getCategory1() {
        return baseCategory1Mapper.selectList(null);
    }
    @AppCache(cacheKey ="category:level:2:#{#args[0]}")
    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {
        return baseCategory2Mapper.selectList(new QueryWrapper<BaseCategory2>().eq("category1_id", category1Id));
    }
    @AppCache(cacheKey ="category:level:3:#{#args[0]}")
    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
        return baseCategory3Mapper.selectList(new QueryWrapper<BaseCategory3>().eq("category2_id", category2Id));
    }

    //获取category1、category2、category3的树形结构
    @AppCache(cacheKey = RedisConstant.CATEGORY_CACHE_KEY)
    @Override
    public List<BaseCategroysTo> getCategoryAndChildrens() {
        //数据固定不变,使用redis缓存
        List<BaseCategroysTo> baseCategroysTos = cacheService.getCacheData(RedisConstant.CATEGORY_CACHE_KEY, new TypeReference<List<BaseCategroysTo>>() {
        });
        if ( baseCategroysTos !=null){
            return baseCategroysTos;
        }
        //为空时回源查询
        baseCategroysTos = baseCategory1Mapper.getCategoryAndChildrens();
        //并将查询结果缓存到redis中
        cacheService.setCacheData(RedisConstant.CATEGORY_CACHE_KEY, baseCategroysTos);
        return baseCategroysTos;

    }

    //获取category1、category2、category3的视图结构
    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {
        return baseCategory1Mapper.getCategoryView(category3Id);

    }
}
